'use strict';

angular.module('homepageApp')
  .controller('MainCtrl', function ($scope, Backendservice) {
		$scope.images = null;
        $scope.posts = null;
        $scope.orderPostBy = 'postDateTime.S';



        $scope.myInterval = 5000;
        var slides = $scope.slides = [];
        $scope.addSlide = function() {
            var newWidth = 600 + slides.length;
            slides.push({
                    image: 'http://placehold.it/900x500/b81414/B20000&text=Lasluisa+Solutions',
                    headertext: 'Lasluisa Solutions',
                    text: 'Solutions are 1% talent and 99% percent hard work'
                },
                {
                    image: 'http://placehold.it/900x500/A81414/A20000&text=Wise+Words',
                    headertext: 'Wise Words',
                    text: '"We are what we repeatedly do. Excellence, then, is not an act, but a habit." - Aristotle'},
                {
                    image: 'http://placehold.it/900x500/981414/920000&text=Cloud+Computing',
                    headertext: 'Cloud Computing',
                    text: 'Moving to the cloud doesn\'t need to be a headache'});
        };

        $scope.addSlide();

        $scope.postOrderOptions = [
            {name:'latest', reverse:true},
            {name:'oldest', reverse:false}
        ];

        $scope.currentPostOrder = 1;
        $scope.changePostOrder = function(){
                $scope.currentPostOrder = $scope.currentPostOrder+1;
                $scope.selectedPostOrder = $scope.postOrderOptions[$scope.currentPostOrder%2];
        }

        $scope.getImages= function() {
            Backendservice.getImagesForPage().then(function (response) {
                $scope.images = response.data;
            });
        }

        $scope.getPosts= function() {
            Backendservice.getPostsForPage().then(function(response){
                $scope.posts = response.data;
            });
        }
        $scope.init = function() {
            $scope.getPosts();
            $scope.getImages();
	        $scope.changePostOrder();
        };
		$scope.init();
  });
